﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using CultuRAM.Models;
using CultuRAM.DAL;
using PagedList;

namespace CultuRAM.Controllers
{
    [Authorize]
    public class CommentController : Controller
    {
        private readonly UnitOfWork _unitOfWork = EventController._unitOfWork;
        private readonly CacheMode _cacheMode = EventController._CacheMode;


        //
        // GET: /Resource/Default

        public ActionResult Default()
        {
            return PartialView("_Default");
        }

        //
        // GET: /Comment/

        public ActionResult _Index(int page, int id)
        {
            var comments = _unitOfWork.CommentRepository.Get(r => r.EventId == id);

            ViewBag.idEv = id;

            return View(comments.ToPagedList(page, 7));
        }

        // 
        // GET: /Comment/

        public PartialViewResult IndexPv(int page, int id)
        {
            var comments = _unitOfWork.CommentRepository.Get(r => r.EventId == id);

            ViewBag.idEv = id;

            return PartialView("_Index", comments.ToPagedList(page, 7));
        }
  

        // 
        // GET: /Comment/Edit/5

        public ActionResult EditPv(int id = 0)
        {
            if (id == 0)
            {
                ViewBag.MsgWarning = new MvcHtmlString("<b>ATENÇÃO:</b><br> Selecione o comentário que pretende editar, clicando numa das linhas da tabela!");
                return PartialView("_Default");
            }

            var comment = _unitOfWork.CommentRepository.GetByID(id);
        
            if (comment == null)
            {
                ViewBag.MsgError = "Ocorreu um erro ao tentar editar o Comentário. Certifique-se que selecionou um comentário!";
                return PartialView("_Default");
            }

            return PartialView("Edit", comment);
        }


        // 
        // POST: /Comment/Edit/5

        [HttpPost]
        public ActionResult EditPv(Comment comment)
        {
            var evId = comment.EventId == 0 ? comment.Event.EventId : comment.EventId;

            comment.Event = _unitOfWork.EventRepository.GetByID(evId);

            //ev.Comments = GetCommentByEventId(evId);
            //comment.EventId = evId;

            if (ModelState.IsValid)
            {
                _unitOfWork.CommentRepository.Update(comment);
                _unitOfWork.Save();

                ViewBag.MsgSuccess = "O Comentário foi alterado com sucesso!";
                return PartialView("_Default");
            }

            return PartialView("Edit", comment);
        }


        // 
        // GET: /Comment/Delete/5

        public ActionResult DeletePv(int id = 0)
        {
            if (id == 0)
            {
                ViewBag.MsgWarning = new MvcHtmlString("<b>ATENÇÃO:</b><br> Selecione o comentário que pretende editar, clicando numa das linhas da tabela!");
                return PartialView("_Default");
            }

            var comment = _unitOfWork.CommentRepository.GetByID(id);


            if (comment == null)
            {
                ViewBag.MsgError = "Ocorreu um erro ao tentar eliminar o Comentário. Certifique-se que selecionou um Comentário!";
                return PartialView("_Default");
            }

            return PartialView("Delete", comment);
        }


        //
        // POST: /Comment/Delete/5

        [HttpPost, ActionName("DeletePv")]
        public PartialViewResult DeleteConfirmedPv(Comment comment)
        {
            comment.EventId = comment.EventId == 0 ? comment.Event.EventId : comment.EventId;

            if (ModelState.IsValid)
            {          
                _unitOfWork.ResourceRepository.Delete(comment.CommentId);
                _unitOfWork.Save();
             
                ViewBag.MsgSuccess = "O Comentário foi eliminado com sucesso!";
                return PartialView("_Default");
            }

            return PartialView("Delete", comment);
        }



        //AUXILIARY METHODS /////////////////////////////////////////////////////////
        #region Auxiliary_Methods

        //Method: Get the list of all Comments by ID - Paged
        public IPagedList<Comment> GetCommentsByEventIdPaged(int eventId, int page = 1)
        {
            return _unitOfWork.CommentRepository.Get(filter: x => x.EventId == eventId).OrderByDescending(o => o.CommentId).ToPagedList(page, 7);
        }

        //Method: Get the list of all Commets by ID
        public IList<Comment> GetCommentByEventId(int eventId, int page = 1)
        {
            return _unitOfWork.CommentRepository.Get(filter: x => x.EventId == eventId).OrderByDescending(o => o.CommentId).ToList();
        }

        #endregion


    }//Class CommentController

}//Namespace CultuRAM.Controllers